home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / ncurses-5.3 / ncurses / widechar / lib_get_wstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-27  |  6.1 KB  |  206 lines

  1. /****************************************************************************
  2.  * Copyright (c) 2002 Free Software Foundation, Inc.                        *
  3.  *                                                                          *
  4.  * Permission is hereby granted, free of charge, to any person obtaining a  *
  5.  * copy of this software and associated documentation files (the            *
  6.  * "Software"), to deal in the Software without restriction, including      *
  7.  * without limitation the rights to use, copy, modify, merge, publish,      *
  8.  * distribute, distribute with modifications, sublicense, and/or sell       *
  9.  * copies of the Software, and to permit persons to whom the Software is    *
  10.  * furnished to do so, subject to the following conditions:                 *
  11.  *                                                                          *
  12.  * The above copyright notice and this permission notice shall be included  *
  13.  * in all copies or substantial portions of the Software.                   *
  14.  *                                                                          *
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
  16.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
  17.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
  18.  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
  19.  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
  20.  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
  21.  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
  22.  *                                                                          *
  23.  * Except as contained in this notice, the name(s) of the above copyright   *
  24.  * holders shall not be used in advertising or otherwise to promote the     *
  25.  * sale, use or other dealings in this Software without prior written       *
  26.  * authorization.                                                           *
  27.  ****************************************************************************/
  28.  
  29. /****************************************************************************
  30.  *  Author: Thomas E. Dickey 2002                                           *
  31.  ****************************************************************************/
  32.  
  33. /*
  34. **    lib_get_wstr.c
  35. **
  36. **    The routine wgetn_wstr().
  37. **
  38. */
  39.  
  40. #include <curses.priv.h>
  41. #include <term.h>
  42.  
  43. MODULE_ID("$Id: lib_get_wstr.c,v 1.4 2002/07/20 19:28:29 tom Exp $")
  44.  
  45. /*
  46.  * This wipes out the last character, no matter whether it was a tab, control
  47.  * or other character, and handles reverse wraparound.
  48.  */
  49. static wchar_t *
  50. WipeOut(WINDOW *win, int y, int x, wchar_t * first, wchar_t * last, bool echoed)
  51. {
  52.     if (last > first) {
  53.     *--last = '\0';
  54.     if (echoed) {
  55.         int y1 = win->_cury;
  56.         int x1 = win->_curx;
  57.  
  58.         wmove(win, y, x);
  59.         waddwstr(win, first);
  60.         getyx(win, y, x);
  61.         while (win->_cury < y1
  62.            || (win->_cury == y1 && win->_curx < x1))
  63.         waddch(win, (chtype) ' ');
  64.  
  65.         wmove(win, y, x);
  66.     }
  67.     }
  68.     return last;
  69. }
  70.  
  71. NCURSES_EXPORT(int)
  72. wgetn_wstr(WINDOW *win, wint_t * str, int maxlen)
  73. {
  74.     TTY buf;
  75.     bool oldnl, oldecho, oldraw, oldcbreak;
  76.     wint_t erasec;
  77.     wint_t killc;
  78.     wchar_t *oldstr;
  79.     wchar_t *tmpstr;
  80.     wint_t ch;
  81.     int y, x, code;
  82.  
  83.     T((T_CALLED("wgetn_wstr(%p,%p, %d)"), win, str, maxlen));
  84.  
  85.     if (!win)
  86.     returnCode(ERR);
  87.  
  88.     _nc_get_tty_mode(&buf);
  89.  
  90.     oldnl = SP->_nl;
  91.     oldecho = SP->_echo;
  92.     oldraw = SP->_raw;
  93.     oldcbreak = SP->_cbreak;
  94.     nl();
  95.     noecho();
  96.     noraw();
  97.     cbreak();
  98.  
  99.     erasec = erasechar();
  100.     killc = killchar();
  101.  
  102.     assert(sizeof(wchar_t) == sizeof(wint_t));
  103.     oldstr = (wchar_t *) str;
  104.     tmpstr = (wchar_t *) str;
  105.  
  106.     getyx(win, y, x);
  107.  
  108.     if (is_wintouched(win) || (win->_flags & _HASMOVED))
  109.     wrefresh(win);
  110.  
  111.     while ((code = wget_wch(win, &ch)) != ERR) {
  112.     if (code == KEY_CODE_YES) {
  113.         /*
  114.          * Some terminals (the Wyse-50 is the most common) generate a \n
  115.          * from the down-arrow key.  With this logic, it's the user's
  116.          * choice whether to set kcud=\n for wget_wch(); terminating
  117.          * *getn_wstr() with \n should work either way.
  118.          */
  119.         if (ch == '\n'
  120.         || ch == '\r'
  121.         || ch == KEY_DOWN
  122.         || ch == KEY_ENTER) {
  123.         if (oldecho == TRUE
  124.             && win->_cury == win->_maxy
  125.             && win->_scroll)
  126.             wechochar(win, (chtype) '\n');
  127.         break;
  128.         }
  129.         if (ch == erasec || ch == KEY_LEFT || ch == KEY_BACKSPACE) {
  130.         if (tmpstr > oldstr) {
  131.             tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
  132.         }
  133.         } else if (ch == killc) {
  134.         while (tmpstr > oldstr) {
  135.             tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
  136.         }
  137.         } else {
  138.         beep();
  139.         }
  140.     } else if (maxlen >= 0 && tmpstr - oldstr >= maxlen) {
  141.         beep();
  142.     } else {
  143.         *tmpstr++ = ch;
  144.         if (oldecho == TRUE) {
  145.         int oldy = win->_cury;
  146.         cchar_t tmp;
  147.  
  148.         setcchar(&tmp, tmpstr - 1, A_NORMAL, 0, NULL);
  149.         if (wadd_wch(win, &tmp) == ERR) {
  150.             /*
  151.              * We can't really use the lower-right corner for input,
  152.              * since it'll mess up bookkeeping for erases.
  153.              */
  154.             win->_flags &= ~_WRAPPED;
  155.             waddch(win, (chtype) ' ');
  156.             tmpstr = WipeOut(win, y, x, oldstr, tmpstr, oldecho);
  157.             continue;
  158.         } else if (win->_flags & _WRAPPED) {
  159.             /*
  160.              * If the last waddch forced a wrap & scroll, adjust our
  161.              * reference point for erasures.
  162.              */
  163.             if (win->_scroll
  164.             && oldy == win->_maxy
  165.             && win->_cury == win->_maxy) {
  166.             if (--y <= 0) {
  167.                 y = 0;
  168.             }
  169.             }
  170.             win->_flags &= ~_WRAPPED;
  171.         }
  172.         wrefresh(win);
  173.         }
  174.     }
  175.     }
  176.  
  177.     win->_curx = 0;
  178.     win->_flags &= ~_WRAPPED;
  179.     if (win->_cury < win->_maxy)
  180.     win->_cury++;
  181.     wrefresh(win);
  182.  
  183.     /* Restore with a single I/O call, to fix minor asymmetry between
  184.      * raw/noraw, etc.
  185.      */
  186.     SP->_nl = oldnl;
  187.     SP->_echo = oldecho;
  188.     SP->_raw = oldraw;
  189.     SP->_cbreak = oldcbreak;
  190.  
  191.     (void) _nc_set_tty_mode(&buf);
  192.  
  193.     *tmpstr = 0;
  194.     if (code == ERR) {
  195.     if (tmpstr == oldstr) {
  196.         *tmpstr++ = (wchar_t)WEOF;
  197.         *tmpstr = 0;
  198.     }
  199.     returnCode(ERR);
  200.     }
  201.  
  202.     T(("wgetn_wstr returns %s", _nc_viswbuf(oldstr)));
  203.  
  204.     returnCode(OK);
  205. }
  206.